import React from "react";

import PageContent from '../PageContent'

import { Badge, Card, Modal } from 'antd';

import { ajax, showResultMessage, getLocalMessage } from '../common';

import { UIDataTableView } from '../uicomponents/UIDataComponentView';

import UIForm from '../uicomponents/UIForm';

import UICardTabs from '../uicomponents/UICardTabs';


/** 
 * 系统配置管理
*/
export default class SysConfigManageApp extends React.Component {

    render() {

        return (
            <PageContent pageInstance={this} {...this.props}>
                <SysConfigPanel />
            </PageContent>
        );
    }
}

var WarpConfigPanel = (SysConfigComponent, props) => {
    class WarpedComponent extends React.Component {
        constructor(props) {
            super(props);
            this.state = {
            };
        }

        loadConfig = () => {
            ajax('SYSTEM/APP/getSystemConfig', { configType: props.configType }, (data) => {
                this.formRef.setFormData(data);
            });
        }

        componentDidMount() {
            this.loadConfig();
        }

        actionHandler = (actionEvent) => {
            const { formRef } = this;
            return {
                doSave: () => {
                    const result = {};
                    formRef.getFormData((formData) => {
                        ajax('SYSTEM/APP/saveSystemConfig/' + props.configType, formData).then((data) => {
                            if (data) {
                                result.success = true;
                                result.message = 'message.saveSuccess';
                            }
                        }).catch((reason) => {
                            result.success = true;
                            result.message = reason;
                        }).finally(() => {
                            showResultMessage(result);
                        });
                    });
                }
            };
        }

        render() {
            return (
                <SysConfigComponent
                    formConfigId={props.formConfigId}
                    actionGroupId="saveActionGroup"
                    wrappedComponentRef={(inst) => this.formRef = inst}
                    actionHandler={this.actionHandler}
                />
            );
        }
    }

    return WarpedComponent;
}

/** 
 * 系统应用配置
*/
const SysAppConfigPanel = WarpConfigPanel(UIForm, { formConfigId: 'sysAppConfigForm', configType: 'APPLICATION' });

/** 
 * 系统邮件配置
*/
const SysMailConfigPanel = WarpConfigPanel(UIForm, { formConfigId: 'sysMailConfigForm', configType: 'MAIL_SERVER' });


/** 
 * 系统配置
*/
class SysConfigPanel extends React.Component {

    onTabChange = (tabName) => {
        switch (tabName) {
            case 'sysAppConfigPart':
                this.sysAppConfigRef.loadConfig();
                break;
            case 'sysMailConfigPart':
                this.sysMailConfigRef.loadConfig();
                break;
        }
    }

    render() {

        const tabPanelContentMap = {
            '/SysAppConfigPanel': {

                getComponent: (owner, props) => {
                    return <SysAppConfigPanel ref={(inst) => this.sysAppConfigRef = inst} />;
                }
            },
            '/SysMailConfigPanel': {

                getComponent: (owner, props) => {
                    return <SysMailConfigPanel ref={(inst) => this.sysMailConfigRef = inst} />
                }
            },

        };

        const configProps = {
            tabConfigId: 'sysConfigTab',
            onTabChange: this.onTabChange,
            tabContentMap: tabPanelContentMap,
        };

        return (
            <UICardTabs {...configProps}>
            </UICardTabs>
        );
    }
}